package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserRole;
import com.tsd.system.entity.SysUserRoleExt;
import com.tsd.system.service.SysUserRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysUserRole 控制层
 *
 * @author Hillpool
 * @date 2019/03/12 12:20:51
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class SysUserRoleController extends SystemBaseController {

    private final SysUserRoleService sysUserRoleService;

    @Autowired
    public SysUserRoleController(SysUserRoleService sysUserRoleService) {
        this.sysUserRoleService = sysUserRoleService;
    }

    @ApiOperation(value = "获取id获取用户角色信息")
    @ResponseBody
    @RequestMapping(value = "/getSysUserRoleById.action", method = RequestMethod.POST)
    public HttpResult<SysUserRole> getSysUserRoleById() {
        HttpResult<SysUserRole> hr = new HttpResult<SysUserRole>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            SysUserRole sysUserRole = sysUserRoleService.getSysUserRoleById(id);
            hr.setData(sysUserRole);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "设置用户拥有的角色列表")
    @ResponseBody
    @RequestMapping(value = "/saveRoleList2User.action", method = RequestMethod.POST)
    public HttpResult saveRoleList2User() {
        HttpResult<SysUserRole> hr = new HttpResult<SysUserRole>();
        try {
            SysUser opUser = this.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String userId = HlpUtils.getString(map, "userId");
            Object list = map.get("roleIdList");
            if (list != null && !(list instanceof List)) {
                throw new HlpException("roleIdList 不是list类型");
            }
            List<String> roleIdList = (List<String>) list;
            super.checkEmpty(userId, "缺少参数userId");
            super.checkEmpty(roleIdList, "缺少参数roleIdList");
            sysUserRoleService.saveRoleList2User(userId, roleIdList, true, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "查询某个用户的角色列表")
    @ResponseBody
    @RequestMapping(value = "/findRoleListByUser.action", method = RequestMethod.POST)
    public HttpResult<Object> findRoleListByUser() {
        HttpResult<Object> hr = new HttpResult<Object>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyStringValue(map, params, "userSid", "userSid", true, false);
            HlpUtils.copyStringValue(map, params, "org_type_sid", "org_type_sid", false, false);
            List<SysUserRoleExt> list = sysUserRoleService.findRoleListByUserId(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "保存用户角色信息")
    @ResponseBody
    @RequestMapping(value = "/saveSysUserRole.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysUserRole() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser sysUser = this.getSysUser();
            Map<String, Object> map = super.getParams(request);
            SysUserRole t = JSON.parseObject(JSON.toJSONString(map), SysUserRole.class);
            sysUserRoleService.saveSysUserRole(t, sysUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除用户角色信息")
    @ResponseBody
    @RequestMapping(value = "/deleteSysUserRole.action", method = RequestMethod.POST)
    public HttpResult<String> deleteSysUserRole() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            sysUserRoleService.deleteSysUserRole(id);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询用户角色信息")
    @ResponseBody
    @RequestMapping(value = "/findSysUserRoles.action", method = RequestMethod.POST)
    public HttpResult<List<SysUserRole>> findSysUserRoles() {
        HttpResult<List<SysUserRole>> hr = new HttpResult<List<SysUserRole>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            //String searchValue = (String) map.get("searchValue");
            //if (!HlpUtils.isEmpty(searchValue)) {
            //	params.put("searchValue", "%" + searchValue + "%");
            //}
            PageBean page = new PageBean(map);
            List<SysUserRole> list = sysUserRoleService.findSysUserRoles(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
